<template>
  <div class="login">
    <el-form ref="form" :rules="rules" :model="form" status-icon label-width="80px">
      <img src="../assets/avatar.jpg" alt />
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" placeholder="请输入密码" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "change" },
          {
            min: 5,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "change"
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 5,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    resetForm() {
      //   console.log(this.$refs.form)
      this.$refs.form.resetFields();
    },
    login() {
      this.$refs.form.validate(valid => {
        if(valid) {
            axios({
                url:'http://localhost:8888/api/private/v1/login',
                method: 'post',
                // data: {
                //     username: this.form.username,
                //     password: this.form.password
                // }
                data: this.form
            }).then(res => {
                if(res.data.meta.status === 200) {
                    this.$message({
                        message:'登陆成功',
                        type:'success',
                        duration:1000
                    })
                    this.$router.push('/home')
                }else{
                    this.$message({
                        message:'登录失败',
                        type:'error',
                        duration:1000
                    })
                }
            })
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.login {
  background: #2d434c;
  height: 100%;
  overflow: hidden;
  .el-form {
    width: 400px;
    background: #fff;
    margin: 200px auto;
    padding: 75px 40px 15px 40px;
    border-radius: 20px;
    position: relative;
    img {
      position: absolute;
      top: -65px;
      left: 50%;
      transform: translateX(-50%);
      border-radius: 50%;
      border: 10px solid #fff;
    }
    .el-button:nth-child(2) {
      margin-left: 130px;
    }
  }
}
</style>